﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Globalization;

namespace WebCore
{
    public static class Utils
    {
        public static string CrossSiteScriptingAttackCheck(string input)
        {
            return input.Replace("<script", "[script").Replace("</script>","[/script]");  
        }

       

        /// <summary>
        /// LINQ query
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public static string[] RemoveDuplicateString(string[] stringArray)
        {
            
            return stringArray.Distinct().ToArray();

        }

        /// <summary>
        /// HashSet approach
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string[] RemoveDuplicates(string[] s)
        {
            HashSet<string> set = new HashSet<string>(s);
            string[] result = new string[set.Count];
            set.CopyTo(result);
            return result;
        }

        public static string PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 1; i <= totalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();

                if (i == currentPage)
                    tag.AddCssClass("selected");
                result.AppendLine(tag.ToString());
            }

            return result.ToString();

        }


        /// <summary>
        /// Strips all illegal characters from the specified title.
        /// </summary>
        public static string RemoveIllegalCharacters(string text, bool isEncode)
        {
            if (string.IsNullOrEmpty(text))
                return text;

            text = text.Trim();
            text = text.ToLower();
            text = text.Replace(":", string.Empty);
            text = text.Replace("/", string.Empty);
            text = text.Replace("?", string.Empty);
            text = text.Replace("#", string.Empty);
            text = text.Replace("[", string.Empty);
            text = text.Replace("]", string.Empty);
            text = text.Replace("@", string.Empty);
            text = text.Replace(".", string.Empty);
            text = text.Replace(",", string.Empty);
            text = text.Replace("\"", string.Empty);
            text = text.Replace("&", string.Empty);
            text = text.Replace("'", string.Empty);
            text = text.Replace(" ", "-");
            text = RemoveDiacritics(text);
            text = RemoveExtraHyphen(text);

            if (isEncode)
            {
                return HttpUtility.UrlEncode(text).Replace("%", string.Empty);
            }
            else
            {
                return text; 
            }


            
        }

        private static string RemoveExtraHyphen(string text)
        {
            if (text.Contains("--"))
            {
                text = text.Replace("--", "-");
                return RemoveExtraHyphen(text);
            }

            return text;
        }

        private static String RemoveDiacritics(string text)
        {
            String normalized = text.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < normalized.Length; i++)
            {
                Char c = normalized[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    sb.Append(c);
            }

            return sb.ToString();
        }

        public static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }


        public static string GetTimeAgo(DateTime earlyDateTime, DateTime laterDatetime)
        {
            if (DateTime.Compare(earlyDateTime, laterDatetime) >= 0)
            {
                TimeSpan ts = earlyDateTime.Subtract(laterDatetime);
                return string.Format("{0} days, {1} hours, {2} minutes, {3} seconds",
                    ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
            }
            else
                return "Not valid";
        }

        public static string RemoveWhitespaceWithSplit(string inputString)
        {
            StringBuilder sb = new StringBuilder();

            string[] parts = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v' }, StringSplitOptions.RemoveEmptyEntries);

            int size = parts.Length;
            for (int i = 0; i < size; i++)
                sb.AppendFormat("{0} ", parts[i]);

            return sb.ToString();
        }

    }
}
